﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ls.Utils;
using Ls.Web.Api.Models;
using Ls.Web.Framework;
using Ls.Web.Framework.Permissions;
using Microsoft.AspNetCore.Mvc;

namespace Ls.Web.Api.Controllers
{
    /// <summary>
    /// 账款
    /// </summary>
    public class BookController : BaseController
    {
        AccountBookService bookService = new AccountBookService();

        /// <summary>
        /// 应付账款查询接口
        /// </summary>
        /// <param name="TimeFrom">开始时间</param>
        /// <param name="TimeTo">结束时间</param>
        /// <param name="sortField">排序字段 默认create_time</param>
        /// <param name="sortOrder">培训方式 默认asc</param>
        /// <param name="pageIndex">第几页  默认 0 </param>
        /// <param name="pageSize">每页条数 默认20</param> 
        /// <returns>分页数据</returns>
        /// <remarks> 
        /// 返回值:
        /// 
        ///                 {
        ///                     "resultCode": "200", -- 状态
        ///                     "data": {
        ///                       "total": 1,        -- 总条数
        ///                       "data": [
        ///                         {
        ///                           "fncab_id": 1,   -- 编号
        ///                           "fnca_id": 6,    -- 账户id
        ///                           "carrier_id": 2,  --运营商org_id
        ///                           "carrier_code": "100",  -- 运营商编号
        ///                           "carrier_name": "运营商1（trx测试用）", -- 运营商
        ///                           "store_id": 16,         -- 加盟商org_id
        ///                           "store_code": "000203",  --加盟商编码
        ///                           "store_name": "测师门店",  -- 加盟商
        ///                           "fncab_order": "XS00000002",  -- 单据编号
        ///                           "fncab_type": "销售确认",     --单据类型
        ///                           "amount_begin": -100,         --期初
        ///                           "amount_receivable": -4800,      --应收款
        ///                           "amount_received": 0,             --实收款
        ///                           "amount_end": -4900, -- 期末
        ///                           "create_id": 3,                      --创建人id
        ///                           "create_time": "2018-02-06T17:35:31.134158"   -- 创建日期
        ///                         }
        ///                       ]
        ///                     }
        ///                   }     
        ///                   
        /// </remarks>
        [HttpPost]
        [Route("/book/list")]
        [Permission("App.Book.list")]
        public IActionResult Index(string TimeFrom, string TimeTo,string sortField = "create_time", string sortOrder ="asc",int pageIndex=0,int pageSize = 20)
        {  
            // 只获取当前用户所在的门店 
            string where = " store_id in (select org_id from sys_users where  user_Id = " + this.UserId + ")" ; 
            if (TimeFrom.IsNotNullOrEmpty())
            {
                where += " and create_time >= '" + CommonHelper.ToDateTime(TimeFrom).ToString("yyyy-MM-dd") + "' ";
            }
            if (TimeTo.IsNotNullOrEmpty())
            {
                where += " and create_time <= '" + CommonHelper.ToDateTime(TimeTo).ToString("yyyy-MM-dd 23:59:59") + "' ";
            }
           var pageResult = bookService.Page(@"fncab_id, fnca_id, carrier_id, carrier_code, carrier_name, store_id, store_code, store_name, fncab_order, fncab_type, 
amount_begin * -1 as amount_begin, 
amount_receivable* -1 as amount_receivable,
amount_received* -1 as amount_received,
amount_end* -1 as amount_end,
create_id, create_time",
                @" fnc_account_book a",
                where, 
                "a.create_time");
            return GetSuccessResult(pageResult);
        }
    }
}